from common.Utils import Utils


class CommonNode:
    def __init__(self, size, start, obj):
        self.size = size
        self.start = start
        self.obj = obj
        self.children = []
        self.pre = None
        self.next = None

    def setSize(self, size):
        self.size = size
        return self

    def setStart(self, start):
        self.start = start
        return self

    def setObj(self, obj):
        self.obj = obj
        return self

    def addChild(self, child):
        self.children.append(child)
        return self

    def removeChild(self, child):
        Utils.popIfExist(self.children, child)

    def childAt(self, index):
        return self.children[index]

    def setPre(self, pre):
        self.pre = pre
        return self

    def setNext(self, _next):
        self.next = _next
        return self

    def getNext(self):
        return self.next

    def getPre(self):
        return self.pre

    def getChildren(self):
        return self.children

    def getObj(self):
        return self.obj

    def getStart(self):
        return self.start

    def getSize(self):
        return self.size

    def isFree(self):
        return self.obj is None

    def clear(self):
        self.setObj(None)
        self.children = []

    @staticmethod
    def merge(nodeA, nodeB):
        nodeA.setNext(nodeB.getNext())
        nodeA.setSize(nodeB.getSize() + nodeA.getSize())
        nodeB.clear()
        nodeB.setPre(None)
        nodeB.setNext(None)
        return nodeA
